<template>
  <div class="company">
    <div>
      <el-dialog :title="dialog.title" :visible.sync="dialog.visible">
        <el-form
          :model="company"
          :rules="rules"
          ref="company"
          label-width="100px"
          class="demo-ruleForm"
        >
          <input type="hidden" value="company.id" />
          <el-form-item label="公司名称" prop="name">
            <el-input v-model="company.name"></el-input>
          </el-form-item>
          <el-form-item label="公司电话" prop="tel">
            <el-input v-model="company.tel"></el-input>
          </el-form-item>
          <el-form-item label="公司联系人" prop="contactPerson">
            <el-input v-model="company.contactPerson"></el-input>
          </el-form-item>
          <el-form-item label="公司地址" prop="address">
            <el-input v-model="company.address"></el-input>
          </el-form-item>
          <el-form-item label="公司备注" prop="remark">
            <el-input type="textarea" :rows="3" v-model="company.remark"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('company')">确定</el-button>
            <el-button @click="resetForm('company')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card class="box-card">
          <div class="boxcard-head">
            <el-form :model="queryParam" ref="queryParam" inline class="demo-form-inline" >
              <el-form-item label="公司名称" prop="name">
                <el-input placeholder="公司名称" v-model="queryParam.name"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
                <el-button @click="resetForm('queryParam')">重置</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="addCompany">添加</el-button>
              </el-form-item>
            </el-form>
          </div>
          <div class="boxcard-body">
            <el-table
              :data="list"
              style="width: 100%;"
              row-key="id"
              :header-cell-style="{ 'background-color': '#EEEEEE' }"
            >
              <el-table-column prop="name" label="公司名称"></el-table-column>
              <el-table-column prop="address" label="公司地址"></el-table-column>
              <el-table-column prop="tel" label="公司电话"></el-table-column>
              <el-table-column prop="contactPerson" label="公司联系人"></el-table-column>
              <el-table-column prop="remark" label="公司备注"></el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button type="warning" size="small" @click="updateCompany(scope.row)">修改</el-button>
                  <el-button type="danger" size="small" @click="deleteCompany(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="pagination">
            <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page.currentPage"
              :page-sizes="page.pageSizes"
              :page-size="page.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
            ></el-pagination>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { get, postJson, postXform } from "@/utils/request.js";
export default {
  data() {
    return {
      dialog: {
        title: "添加公司",
        visible: false
      },
      list: [],
      rules: {},
      queryParam:{
        name:null
      },
      page: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
        pageSizes: [10, 20, 50]
      },
      company: {
        id: "",
        name: "",
        address: "",
        tel: "",
        contactPerson: "",
        remark: ""
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.page.currentPage = val;
      this.getList();
    },
    updateCompany(row) {
      this.dialog = { visible: true, title: "修改公司" };
      this.company = {
        id: row.id,
        name: row.name,
        address: row.address,
        tel: row.tel,
        contactPerson: row.contactPerson,
        remark: row.remark
      };
    },
    deleteCompany(row) {
      this.$confirm("是否删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          postXform("/company/deleteCompany", { id: row.id }).then(res => {
            this.$message({ message: "删除成功", type: "success" });
            this.getList();
          });
        })
        .catch(() => {});
    },
    addCompany() {
      this.dialog.visible = true;
      this.dialog.title = "添加公司";
      this.company = {
        id: "",
        name: "",
        address: "",
        tel: "",
        contactPerson: "",
        remark: ""
      };
    },
    async getList() {
      await get("/company/pageCompany", {
        pageNum: this.page.currentPage,
        pageSize: this.page.pageSize,
        name:this.queryParam.name
      }).then(res => {
        this.list = res.data.data.list;
        this.page.total = Number.parseInt(res.data.data.total);
        this.page.currentPage = res.data.data.pageNum;
      });
    },
    submitForm(company) {
      this.$refs.company.validate(valid => {
        if (valid) {
          if (this.company.id === "") {
            postXform("/company/addCompany", this.company).then(res => {
              this.dialog.visible = false;
              this.$message({ message: "添加成功", type: "success" });
              this.getList();
            });
          } else {
            postXform("/company/updateCompany", this.company).then(res => {
              this.dialog.visible = false;
              this.$message({ message: "修改成功", type: "success" });
              this.getList();
            });
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      if (formName == "queryParam") {
        this.getList();
      }
    }
  }
};
</script>

<style lang="less"></style>
